package com.atues.array.romanToint12;



import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class Solution {

    public int rti(String s) {
        int ans = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            switch (c) {
                case'I':
                    if(i+ 1 < s.length() ) {
                        if(s.charAt(i+1) == 'V'){
                            ans+=4;
                            i++;
                            break;

                        } else if (s.charAt(i + 1) == 'X') {
                            ans+=9;
                            i++;
                            break;
                        }
                    }
                    ans+=1;
                    break;
                case 'V':{
                    ans+=5;
                    break;
                }
                case 'X':{
                    if(i+ 1 < s.length() ) {
                        if(s.charAt(i+1) == 'L'){
                            ans+=40;
                            i++;
                            break;

                        } else if (s.charAt(i + 1) == 'C') {
                            ans+=90;
                            i++;
                            break;
                        }
                    }
                    ans+=10;
                    break;
                }
                case 'L':{
                    ans+=50;
                    break;
                }
                case 'C':{
                    if(i+ 1 < s.length() ) {
                        if(s.charAt(i+1) == 'D'){
                            ans+=400;
                            i++;
                            break;
                        } else if (s.charAt(i + 1) == 'M') {
                            ans+=900;
                            i++;
                            break;
                        }
                    }
                    ans+=100;
                    break;
                }
                case 'D':{
                    ans+=500;
                    break;
                }
                case 'M':{
                    ans+=1000;
                    break;
                }

            }
        }
        return ans;
    }

    public String i2r(int num) {
        StringBuilder sb  = new StringBuilder();
        String s = Integer.toString(num);
        char[] c = {'I','X','C','M'};
        char[] c1 = {'V','L','D'};
        for (int i = 0; i < s.length(); i++) {
            int t = s.charAt(i) - '0',n = s.length() - i - 1;
            if(t == 4){
                sb.append(c[n]);
                sb.append(c1[n]);
            } else if (t == 9) {
                sb.append(c[n]);
                sb.append(c[n + 1]);
            } else if (t > 5) {
                sb.append(c1[n]);
                for (int j = 0; j < t - 5; j++) {
                    sb.append(c[n]);
                }
            } else if (t < 4) {
                for (int j = 0; j < t; j++) {
                    sb.append(c[n]);
                }
            }else if(t == 5) {
                sb.append(c1[n]);
            }else {

            }

        }
        return sb.toString();
    }

    public String longestCommonPrefix(String[] strs) {
        int j = 0;
        if(strs[0].length() == 0)
            return "";
        char c ;
        while ( j < strs[0].length() ){
            c = strs[0].charAt(j);
            for(int i = 1  ; i < strs.length ; i ++){
                if(j>= strs[i].length() || strs[i].charAt(j) != c) return strs[0].substring(0, j);
            }
            j++;
        }
        return strs[0].substring(0, j);

    }

    public String rti2(int num) {
        Map<Integer, String> map = new TreeMap<>(Comparator.reverseOrder());
        map.put(1, "I");
        map.put(4, "IV");
        map.put(5, "V");
        map.put(9, "IX");
        map.put(10, "X");
        map.put(40, "XL");
        map.put(50, "L");
        map.put(90, "XC");
        map.put(100, "C");
        map.put(400, "CD");
        map.put(500, "D");
        map.put(900, "CM");
        map.put(1000, "M");
        StringBuilder sb = new StringBuilder();
        for(Map.Entry<Integer, String> entry : map.entrySet()){
            int key = entry.getKey();
            while(num >= key){
                num -= key;
                sb.append(entry.getValue());
            }
        }
        return sb.toString();

    }

    public static void main(String[] args) {
        Solution solution = new Solution();
//        int i = solution.rti("MCMXCIV");
//        System.out.println(solution.rti2(1994));
//        System.out.println(i);
        System.out.println(solution.longestCommonPrefix(new String[]{"flower","flow","flight"}));
    }
}
